package com.qzdsoft.erpcloud.controller.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.basic.Currency;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.basic.CurrencyService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.query.basic.CurrencyInfo;
import com.qzdsoft.erpcloud.vo.query.basic.CurrencyQueryParam;
import com.qzdsoft.utils.CommonSfEnum;
import com.qzdsoft.utils.RequestUtil;
//import com.qzd.erpcloud.util.SessionUtil;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.utils.constant.SystemConstant;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.basic.BasicPreinstallEnum;
import com.qzdsoft.vo.basic.BasicRateTypeEnum;
import com.qzdsoft.vo.basic.BasicTranslateTypeEnum;

/**
 * Created by Admin
 */
@Controller
@RequestMapping("/basic/currency")
public class CurrencyController extends BaseController {

//    @Autowired
//    private RedisHelper redisHelper;

    @Autowired
    private CurrencyService basicCurrencyService;
    
    @Autowired
    CenterCompanyService actCenterCompanyService;

    @Permission(validate=false)
    @RequestMapping("/index")
    public Object index(HttpServletRequest request, Model model) {

        List<EnumPo> preinstallList = new ArrayList<EnumPo>();
        Map<Object, Object> preinstallMap = new HashMap<Object, Object>();
        for(BasicPreinstallEnum e : BasicPreinstallEnum.values()){
            SysUtils.enums(preinstallList, preinstallMap, e.getKey(), e.getVal());
        }

        List<EnumPo> rateTypeList = new ArrayList<EnumPo>();
        Map<Object, Object> rateTypeMap = new HashMap<Object, Object>();
        for(BasicRateTypeEnum e : BasicRateTypeEnum.values()){
            SysUtils.enums(rateTypeList, rateTypeMap, e.getKey(), e.getVal());
        }

        List<EnumPo> translateTypeList = new ArrayList<EnumPo>();
        Map<Object, Object> translateTypeMap = new HashMap<Object, Object>();
        for(BasicTranslateTypeEnum e : BasicTranslateTypeEnum.values()){
            SysUtils.enums(translateTypeList, translateTypeMap, e.getKey(), e.getVal());
        }

        List<EnumPo> sfList = new ArrayList<EnumPo>();
        Map<Object, Object> sfMap = new HashMap<Object, Object>();
        for(CommonSfEnum e : CommonSfEnum.values()){
            SysUtils.enums(sfList, sfMap, e.getKey(), e.getVal());
        }

        model.addAttribute("preinstallList", preinstallList);
        model.addAttribute("preinstallMap", preinstallMap);

        model.addAttribute("rateTypeList", rateTypeList);
        model.addAttribute("rateTypeMap", rateTypeMap);

        model.addAttribute("translateTypeList", translateTypeList);
        model.addAttribute("translateTypeMap", translateTypeMap);

        model.addAttribute("sfList", sfList);
        model.addAttribute("sfMap", sfMap);

        model.addAttribute("actCenterCompanyMap", actCenterCompanyService.getAllCompany());

        return "basic/currency/index";
    }
    /**
     * 编辑与新增
     * @param CompanyId
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView edit(Integer CompanyId,Integer id) {
        ModelAndView mv = new ModelAndView();
        
        List<EnumPo> preinstallList = new ArrayList<EnumPo>();
        Map<Object, Object> preinstallMap = new HashMap<Object, Object>();
        for(BasicPreinstallEnum e : BasicPreinstallEnum.values()){
            SysUtils.enums(preinstallList, preinstallMap, e.getKey(), e.getVal());
        }

        List<EnumPo> rateTypeList = new ArrayList<EnumPo>();
        Map<Object, Object> rateTypeMap = new HashMap<Object, Object>();
        for(BasicRateTypeEnum e : BasicRateTypeEnum.values()){
            SysUtils.enums(rateTypeList, rateTypeMap, e.getKey(), e.getVal());
        }

        List<EnumPo> translateTypeList = new ArrayList<EnumPo>();
        Map<Object, Object> translateTypeMap = new HashMap<Object, Object>();
        for(BasicTranslateTypeEnum e : BasicTranslateTypeEnum.values()){
            SysUtils.enums(translateTypeList, translateTypeMap, e.getKey(), e.getVal());
        }

        List<EnumPo> sfList = new ArrayList<EnumPo>();
        Map<Object, Object> sfMap = new HashMap<Object, Object>();
        for(CommonSfEnum e : CommonSfEnum.values()){
            SysUtils.enums(sfList, sfMap, e.getKey(), e.getVal());
        }

        mv.addObject("preinstallList", preinstallList);
        mv.addObject("preinstallMap", preinstallMap);

        mv.addObject("rateTypeList", rateTypeList);
        mv.addObject("rateTypeMap", rateTypeMap);

        mv.addObject("translateTypeList", translateTypeList);
        mv.addObject("translateTypeMap", translateTypeMap);

        mv.addObject("sfList", sfList);
        mv.addObject("sfMap", sfMap);

        mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
        
        if(null != id){
            Currency currency = basicCurrencyService.selectById(id);
            mv.addObject("currency", currency);
            mv.setViewName("basic/currency/edit");
        }else {
            mv.setViewName("basic/currency/add");
        }
        return mv;
    }
    /**
     * 编辑保存
     * @param request
     * @return
     */
    @ResponseBody
    @SysLog(description="编辑币种设置保存")
    @RequestMapping("/edit_execute")
    public Object editExecute(HttpServletRequest request) {
//        return basicCurrencyService.saveOrUpd(RequestUtil.toInt(request, SystemConstant.idKey), SessionUtil.getLoggedCompanyId(request, RequestUtil.toInt(request, "company_id")), request.getParameter("name"),
//                request.getParameter("decode"), request.getParameter("symbol"), RequestUtil.toInt(request, "is_main"), RequestUtil.toInt(request, "is_preinstall"),
//                RequestUtil.toInt(request, "exchang_rate_type"), RequestUtil.toFloat(request, "exchang_rate"), RequestUtil.toFloat(request, "max_translate"),
//                RequestUtil.toInt(request, "translate_type"), request.getParameter("remark")
//        );
        return basicCurrencyService.saveOrUpd(RequestUtil.toInt(request, SystemConstant.idKey),RequestUtil.toInt(request, "company_id"), request.getParameter("name"),
              request.getParameter("decode"), request.getParameter("symbol"), RequestUtil.toInt(request, "is_main"), RequestUtil.toInt(request, "is_preinstall"),
              RequestUtil.toInt(request, "exchang_rate_type"), RequestUtil.toFloat(request, "exchang_rate"), RequestUtil.toFloat(request, "max_translate"),
              RequestUtil.toInt(request, "translate_type"), request.getParameter("remark")
      );
    }
    /**
     * 新增保存
     * @param request
     * @return
     */
    @ResponseBody
    @SysLog(description="新增币种设置保存")
    @RequestMapping("/create_execute")
    public Object createExecute(HttpServletRequest request) {
//        return basicCurrencyService.saveOrUpd(RequestUtil.toInt(request, SystemConstant.idKey), SessionUtil.getLoggedCompanyId(request, RequestUtil.toInt(request, "company_id")), request.getParameter("name"),
//                request.getParameter("decode"), request.getParameter("symbol"), RequestUtil.toInt(request, "is_main"), RequestUtil.toInt(request, "is_preinstall"),
//                RequestUtil.toInt(request, "exchang_rate_type"), RequestUtil.toFloat(request, "exchang_rate"), RequestUtil.toFloat(request, "max_translate"),
//                RequestUtil.toInt(request, "translate_type"), request.getParameter("remark")
//        );
        return basicCurrencyService.saveOrUpd(RequestUtil.toInt(request, SystemConstant.idKey),RequestUtil.toInt(request, "company_id"), request.getParameter("name"),
                request.getParameter("decode"), request.getParameter("symbol"), RequestUtil.toInt(request, "is_main"), RequestUtil.toInt(request, "is_preinstall"),
                RequestUtil.toInt(request, "exchang_rate_type"), RequestUtil.toFloat(request, "exchang_rate"), RequestUtil.toFloat(request, "max_translate"),
                RequestUtil.toInt(request, "translate_type"), request.getParameter("remark")
                );
    }

    /**
     * 根据主键删除
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @SysLog(description="删除币种设置")
    @RequestMapping("/del_execute")
    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        return basicCurrencyService.del(id);
    }

    @ResponseBody
    @SysLog(description="查询币种设置")
    @RequestMapping("/data")
    public LayTableResponse<CurrencyInfo> data(HttpServletRequest request, CurrencyQueryParam param) {
//        Integer companyId = SessionUtil.getLoggedCompanyId(request);
//        if(null != companyId){
//            param.setCompanyId(companyId);
//        }

        return basicCurrencyService.selectData(param.getName(), param.getCompanyId(), param.getIsPreinstall(), param.getExchangRateType(), param.getTranslateType(), param.getPage(), param.getLimit());
    }
    
    /**
     * 明细
     * @return
     */
    @RequestMapping("/detail")
    public ModelAndView detail(Integer id) {
        ModelAndView mv = new ModelAndView();
        Currency currency = basicCurrencyService.selectById(id);
        mv.addObject("currency", currency);
        mv.setViewName("basic/currency/view");
        return mv;
    }
    
    
}
